import Main from '@/components/main'
import StartMain from '@/cus_view/startpage/index'
import config from '@/config'
// import parentView from '@/components/parent-view'

/**
 * iview-admin中meta除了原生参数外可配置的参数:
 * meta: {
 *  title: { String|Number|Function }
 *         显示在侧边栏、面包屑和标签栏的文字
 *         使用'{{ 多语言字段 }}'形式结合多语言使用，例子看多语言的路由配置;
 *         可以传入一个回调函数，参数是当前路由对象，例子看动态路由和带参路由
 *  hideInBread: (false) 设为true后此级路由将不会出现在面包屑中，示例看QQ群路由配置
 *  hideInMenu: (false) 设为true后在左侧菜单不会显示该页面选项
 *  notCache: (false) 设为true后页面在切换标签后不会缓存，如果需要缓存，无需设置这个字段，而且需要设置页面组件name属性和路由配置的name一致
 *  access: (null) 可访问该页面的权限数组，当前路由设置的权限会影响子路由
 *  icon: (-) 该页面在左侧菜单、面包屑和标签导航处显示的图标，如果是自定义图标，需要在图标名称前加下划线'_'
 *  beforeCloseName: (-) 设置该字段，则在关闭当前tab页时会去'@/router/before-close.js'里寻找该字段名对应的方法，作为关闭前的钩子函数
 * }
 */
import store from '@/store'

const jumpTo = () => {
  const { operatorId, stationId } = store.state.user
  if (operatorId != 0 && stationId == 0) {
    return { name: 'that_front_overview', params: { id: operatorId } }
  } else if (operatorId != 0 && stationId != 0) {
    return { name: 'stationOverview', params: { id: stationId } }
  } else {
    return { name: 'front_overview' }
  }
}

const routers = [
  {
    path: '/login',
    name: 'login',
    meta: {
      title: 'Login - 登录',
      hideInMenu: true
    },
    component: () => import('@/view/login/login.vue')
  },
  {
    path: '/startpage',
    name: 'startpage',
    component: StartMain,
    meta: {
      hideInMenu: true,
      notCache: true
    },
    redirect: jumpTo()
  },
  {
    path: '/screen/station/:id',
    name: 'screen',
    meta: {
      hideInMenu: true,
      title: '充电站可视化大屏',
      notCache: true
    },
    component: () => import('@/cus_view/screen/views/screen.vue'),
    props: true
  },
  {
    path: '/screen/operator/:id',
    name: 'screen-operator',
    meta: {
      hideInMenu: true,
      title: '运营商可视化大屏',
      notCache: true
    },
    component: () => import('@/cus_view/screen-operator/views/screen.vue'),
    props: true
  },
  {
    path: '/screen/admin',
    name: 'screen-admin',
    meta: {
      hideInMenu: true,
      title: '超级管理员可视化大屏',
      notCache: true
    },
    component: () => import('@/cus_view/screen-admin/views/screen.vue'),
    props: true
  },
  {
    path: '/front',
    name: 'frontMain',
    component: StartMain,
    meta: {
      hideInBread: true
    },
    children: [
      {
        path: 'overview',
        name: 'front_overview',
        meta: {
          title: '运营商管理',
          path: '/front/overview'
        },
        component: () => import('@/cus_view/startpage/overview')
      },
      {
        path: 'overview/:id',
        name: 'that_front_overview',
        meta: {
          title: '运营商管理',
          path: '/front/overview/:id'
        },
        props: true,
        component: () => import('@/cus_view/startpage/overview')
      },
      {
        path: 'station/:id',
        name: 'stationOverview',
        meta: {
          title: '充电站概览',
          path: '/front/station/:id',
          noclose: true
        },
        component: () => import('@/cus_view/startpage/stationList2'),
        props: true
      },
      {
        path: 'gun/:pileId',
        name: 'gunDetail',
        meta: {
          title: '枪详情',
          path: '/front/gun/:pileId'
        },
        component: () => import('@/cus_view/startpage/gunInfo'),
        props: true
      }
    ]
  },
  {
    path: '/',
    component: Main,
    name: 'root',
    meta: {
      hideInMenu: true,
      notCache: true
    },
    redirect: { name: config.home }
  },
  {
    path: '/pm',
    name: 'pm',
    component: Main,
    meta: {
      hideInBread: true,
      icon: 'ios-aperture',
      title: '权限管理'
    },
    icon: 'ios-aperture',
    children: [
      {
        path: 'menu',
        name: 'menu_manage',
        icon: 'ios-aperture',
        meta: {
          icon: 'ios-aperture',
          title: '菜单管理',
          path: '/pm/menu'
        },
        component: () => import('@/cus_view/menumanage/menu.vue')
      },
      {
        path: 'defaultRole',
        name: 'default_role_manage',
        icon: 'ios-aperture',
        meta: {
          icon: 'ios-aperture',
          title: '默认角色管理',
          path: '/pm/defaultRole'
        },
        component: () => import('@/cus_view/menumanage/defaultRole.vue')
      },
      {
        path: 'modifyPassword',
        name: 'modify_password',
        icon: 'ios-aperture',
        meta: {
          icon: 'ios-aperture',
          title: '修改密码',
          path: '/pm/modifyPassword'
        },
        component: () => import('@/cus_view/menumanage/modifyPassword.vue')
      }
    ]
  },
  {
    path: '/station',
    name: 'station',
    component: Main,
    meta: {
      icon: 'md-basket',
      title: '充电站管理'
    },
    children: [
      {
        path: 'stationManage',
        name: 'station_manage',
        meta: {
          icon: 'ios-home',
          title: '站信息管理',
          hideInMenu: true,
          path: '/station/stationManage',
        },
        component: () => import('@/cus_view/station/station_manage.vue')
      },
      {
        path: 'pileManage',
        name: 'pile_manage',
        meta: {
          icon: 'ios-battery-charging',
          title: '充电桩管理',
          hideInMenu: true,
          path: '/station/pileManage',
        },
        component: () => import('@/cus_view/station/pile_manage.vue')
      },
      {
        path: 'vinInfo',
        name: 'vin_bus_info',
        meta: {
          icon: 'ios-car',
          title: 'VIN车牌号管理',
          hideInMenu: true,
          path: '/station/vinInfo',
        },
        component: () => import('@/cus_view/station/vinInfo.vue')
      },
      {
        path: 'stationUserQuery/:id',
        name: 'station_user_query',
        meta: {
          title: '充电站用户查询',
          hideInMenu: true,
          path: '/station/stationUserQuery/:id',
        },
        component: () => import('@/cus_view/station/station_user_query.vue')
      },
      {
        path: 'stationPriceInfo',
        name: 'station_price_info',
        meta: {
          icon: 'ios-cog',
          title: '充电站电价信息',
          hideInMenu: true,
          path: '/station/stationPriceInfo',
        },
        component: () => import('@/cus_view/station/stationPriceInfo.vue')
      }
    ]
  },
  {
    path: '/agent',
    name: 'agent',
    component: Main,
    meta: {
      // hideInBread: true
      icon: 'md-basket',
      title: '运营商信息管理',
      useTitle: 'agent_info'
    },
    // 当前菜单下只显示如下子菜单，但是其他子菜单仍有权限访问
    // 比如： 当前菜单下只显示“运营商信息管理”菜单，其他子菜单不显示
    // 值为空时，显示所有子菜单，可选值为children内部name值
    // showMenus: ['agent_info'],
    children: [
      {
        path: 'info',
        name: 'agent_info',
        meta: {
          icon: 'md-basket',
          title: '运营商信息管理',
          path: '/agent/info'
        },
        component: () => import('@/cus_view/agent/info.vue')
      },
      {
        path: 'userQuery/:id',
        name: 'user_query',
        meta: {
          title: '运营商用户查询',
          hideInMenu: true,
          path: '/agent/userQuery/:id'
        },
        props: true,
        component: () => import('@/cus_view/agent/user_query.vue')
      },
      {
        path: 'chargeStation',
        name: 'charge_station',
        meta: {
          title: '充电站查询',
          hideInMenu: true,
          path: '/agent/chargeStation'
        },
        component: () => import('@/cus_view/agent/charge_station.vue')
      },
      {
        path: 'pileQuery',
        name: 'pile_query',
        meta: {
          title: '充电桩查询',
          hideInMenu: true,
          path: '/agent/pileQuery'
        },
        component: () => import('@/cus_view/agent/pile_query.vue')
      },
      {
        path: 'userEdit',
        name: 'user_edit',
        meta: {
          icon: 'ios-home',
          title: '运营商信息编辑',
          hideInMenu: true,
          path: '/agent/userEdit',
        },
        component: () => import('@/cus_view/agent/user_edit.vue')
      },
      {
        path: 'stationManage',
        name: 'agent_station_manage',
        meta: {
          icon: 'ios-battery-charging',
          title: '充电站管理',
          hideInMenu: true,
          path: '/agent/stationManage',
        },
        component: () => import('@/cus_view/agent/stationManage.vue')
      }
    ]
  },
  {
    path: '/custom',
    name: 'custom',
    component: Main,
    meta: {
      // hideInBread: true
      icon: 'md-contact',
      title: '客户信息查询'
    },
    children: [
      {
        path: 'info',
        name: 'custom_info',
        meta: {
          icon: 'md-contact',
          title: '客户信息查询',
          path: '/custom/info'
        },
        component: () => import('@/cus_view/custom/info.vue')
      },
      {
        path: 'iclist/:customId',
        name: 'ic_card_list',
        meta: {
          icon: 'md-contact',
          title: 'IC卡管理',
          path: '/custom/iclist/:customId',
          hideInMenu: true
        },
        component: () => import('@/cus_view/custom/iclist.vue'),
        props: true
      },
      {
        path: 'recharge_log',
        name: 'rechargelog',
        meta: {
          icon: 'md-contact',
          title: '充值记录查询',
          path: '/custom/recharge_log'
        },
        component: () => import('@/cus_view/custom/rechargelog.vue')
      }
    ]
  },
  {
    path: '/user_manage',
    name: 'user_manage',
    component: Main,
    meta: {
      icon: 'md-people',
      title: '用户管理'
    },
    children: [
      {
        path: 'app_user_manage',
        name: 'app_user_manage',
        meta: {
          icon: 'md-apps',
          title: 'APP用户管理',
          path: '/user_manage/app_user_manage'
        },
        component: () => import('@/cus_view/usermanage/app_user_manage.vue')
      },
      {
        path: 'app_suggest_feedback',
        name: 'app_suggest_feedback',
        meta: {
          icon: 'md-at',
          title: 'APP意见反馈',
          path: '/user_manage/app_suggest_feedback'
        },
        component: () =>
          import('@/cus_view/usermanage/app_suggest_feedback.vue')
      },
      {
        path: 'login_user_manage',
        name: 'login_user_manage',
        meta: {
          icon: 'md-log-in',
          title: '登陆用户管理',
          path: '/user_manage/login_user_manage'
        },
        component: () => import('@/cus_view/usermanage/login_user_manage.vue')
      },
      {
        path: 'login_user_manage/:stationId',
        name: 'login_user_manage_by_stationId',
        meta: {
          icon: 'md-log-in',
          title: '登陆用户管理',
          path: '/user_manage/login_user_manage/:stationId'
        },
        component: () => import('@/cus_view/usermanage/login_user_manage.vue'),
        props: true
      },
      {
        path: 'login_rule_manage',
        name: 'login_rule_manage',
        meta: {
          icon: 'md-code',
          title: '登陆用户角色管理',
          path: '/user_manage/login_rule_manage'
        },
        component: () => import('@/cus_view/usermanage/user_role_manage.vue')
      },
      {
        path: 'login_log',
        name: 'login_log',
        meta: {
          icon: 'md-document',
          title: '登陆日志',
          path: '/user_manage/login_log'
        },
        component: () => import('@/cus_view/usermanage/login_log.vue')
      },
      {
        path: 'login_msg',
        name: 'login_msg',
        meta: {
          icon: 'md-alert',
          title: '系统消息',
          path: '/user_manage/login_msg'
        },
        component: () => import('@/cus_view/usermanage/system_msg.vue')
      }
    ]
  },
  {
    path: '/record_query',
    name: 'record_query',
    component: Main,
    meta: {
      icon: 'md-recording',
      title: '记录查询'
    },
    children: [
      {
        path: 'charge_history',
        name: 'charge_history',
        meta: {
          icon: 'ios-map',
          title: '充电记录查询',
          path: '/record_query/charge_history'
        },
        component: () => import('@/cus_view/record/charge_history.vue')
      },
      {
        path: 'upload_history',
        name: 'upload_history',
        meta: {
          icon: 'md-cloud-upload',
          title: '桩定时上报历史记录',
          path: '/record_query/upload_history'
        },
        component: () => import('@/cus_view/record/upload_history.vue')
      },
      {
        path: 'deal_history',
        name: 'deal_history',
        meta: {
          icon: 'logo-usd',
          title: '交易记录',
          path: '/record_query/deal_history'
        },
        component: () => import('@/cus_view/record/deal_history.vue')
      },
      {
        path: 'error_history',
        name: 'error_history',
        meta: {
          icon: 'ios-bug',
          title: '故障桩历史记录',
          path: '/record_query/error_history'
        },
        component: () => import('@/cus_view/record/error_history.vue')
      },
      {
        path: 'query_recharge_log',
        name: 'query_recharge_log',
        meta: {
          icon: 'ios-pulse',
          title: '查询充值记录',
          path: '/record_query/query_recharge_log'
        },
        component: () => import('@/cus_view/record/query_recharge_log.vue')
      },
      {
        path: 'pile_log_record',
        name: 'pile_log_record',
        meta: {
          icon: 'md-map',
          title: '充电过程查询',
          path: '/record_query/pile_log_record'
        },
        component: () => import('@/cus_view/record/pile_log_record.vue')
      }
    ]
  },
  {
    path: '/stats_statistic',
    name: 'stats_statistic',
    component: Main,
    meta: {
      icon: 'ios-stats',
      title: '统计分析'
    },
    children: [
      {
        path: 'stats',
        name: 'stats_income',
        meta: {
          icon: 'md-list',
          title: '汇总统计-按年',
          path: '/stats_statistic/stats'
        },
        component: () => import('@/cus_view/statistic/info.vue')
      },
      {
        path: 'stats_agent',
        name: 'stats_agent_income',
        meta: {
          icon: 'logo-playstation',
          title: '汇总统计--运营商',
          path: '/stats_statistic/stats_agent'
        },
        component: () => import('@/cus_view/statistic/agent.vue')
      },
      {
        path: 'stats_bus',
        name: 'stats_bus_income',
        meta: {
          icon: 'ios-car',
          title: '汇总统计--各车辆',
          path: '/stats_statistic/stats_bus'
        },
        component: () => import('@/cus_view/statistic/car.vue')
      },
      {
        path: 'stats_charge_type',
        name: 'stats_charge_type',
        meta: {
          icon: 'ios-aperture',
          title: '汇总统计--充电类别',
          path: '/stats_statistic/stats_charge_type'
        },
        component: () => import('@/cus_view/statistic/charge_type.vue')
      },
      {
        path: 'stats_month_type',
        name: 'stats_month_type',
        meta: {
          icon: 'md-calendar',
          title: '汇总统计--按月',
          path: '/stats_statistic/stats_month_type'
        },
        component: () => import('@/cus_view/statistic/month_type.vue')
      },
      {
        path: 'stats_no_card',
        name: 'stats_no_card',
        meta: {
          icon: 'md-card',
          title: '汇总统计--未刷卡充电统计',
          path: '/stats_statistic/stats_no_card'
        },
        component: () => import('@/cus_view/statistic/no_card.vue')
      },
      {
        path: 'app_income',
        name: 'app_income',
        meta: {
          icon: 'ios-phone-portrait',
          title: '汇总统计--手机端',
          path: '/stats_statistic/app_income'
        },
        component: () => import('@/cus_view/statistic/stats_app_income.vue')
      },
      {
        path: 'phone_income',
        name: 'phone_income',
        meta: {
          icon: 'ios-flag',
          title: '汇总统计--手机端收入（按站）',
          path: '/stats_statistic/phone_income'
        },
        component: () => import('@/cus_view/statistic/phone_income.vue')
      },
      {
        path: 'phone_user_income',
        name: 'phone_user_income',
        meta: {
          icon: 'ios-person',
          title: '汇总统计--手机端收入（按用户）',
          path: '/stats_statistic/phone_user_income'
        },
        component: () => import('@/cus_view/statistic/phone_user_income.vue')
      },
      {
        path: 'stats_24h_type',
        name: 'stats_24h_type',
        meta: {
          icon: 'ios-clock',
          title: '汇总统计--24时段',
          path: '/stats_statistic/stats_24h_type'
        },
        component: () => import('@/cus_view/statistic/stats_24h_type.vue')
      },
      {
        path: 'stats_day_type',
        name: 'stats_day_type',
        meta: {
          icon: 'ios-clock',
          title: '汇总统计--按日',
          path: '/stats_statistic/stats_day_type'
        },
        component: () => import('@/cus_view/statistic/day_type.vue')
      },
      {
        path: 'stats_pile',
        name: 'stats_pile',
        meta: {
          icon: 'md-calendar',
          title: '汇总统计--各充电桩',
          path: '/stats_statistic/stats_pile'
        },
        component: () => import('@/cus_view/statistic/stats_pile.vue')
      },
      {
        path: 'stats_station',
        name: 'stats_station',
        meta: {
          icon: 'ios-battery-charging',
          title: '汇总统计--各充电站',
          path: '/stats_statistic/stats_station'
        },
        component: () => import('@/cus_view/statistic/stats_station.vue')
      },
      {
        path: 'stats_iccard',
        name: 'stats_iccard',
        meta: {
          icon: 'ios-card',
          title: '汇总统计--各IC卡+按年',
          path: '/stats_statistic/stats_iccard'
        },
        component: () => import('@/cus_view/statistic/stats_iccard.vue')
      },
      {
        path: 'stats_iccard_month',
        name: 'stats_iccard_month',
        meta: {
          icon: 'ios-card',
          title: '汇总统计--各IC卡+按月',
          path: '/stats_statistic/stats_iccard_month'
        },
        component: () => import('@/cus_view/statistic/stats_iccard_month.vue')
      },
      {
        path: 'stats_iccard_day',
        name: 'stats_iccard_day',
        meta: {
          icon: 'ios-card',
          title: '汇总统计--各IC卡+按日',
          path: '/stats_statistic/stats_iccard_day'
        },
        component: () => import('@/cus_view/statistic/stats_iccard_day.vue')
      },
      {
        path: 'stats_kc',
        name: 'stats_kc',
        meta: {
          icon: 'logo-usd',
          title: '汇总统计--开卡充值',
          path: '/stats_statistic/stats_kc'
        },
        component: () => import('@/cus_view/statistic/stats_kc.vue')
      }
    ]
  },
  {
    path: '/exportExcelDemo',
    name: 'exportExcelDemo',
    meta: {
      hideInMenu: true
    },
    component: () => import('@/cus_view/exportExcel.vue')
  },
  {
    path: '/401',
    name: 'error_401',
    meta: {
      hideInMenu: true
    },
    component: () => import('@/view/error-page/401.vue')
  },
  {
    path: '/500',
    name: 'error_500',
    meta: {
      hideInMenu: true
    },
    component: () => import('@/view/error-page/500.vue')
  },
  {
    path: '*',
    name: 'error_404',
    meta: {
      hideInMenu: true
    },
    component: () => import('@/view/error-page/404.vue')
  }
]

const rootMenu = []
const menuList = []
const allMenus = []

const getAllMenus = (routers) => {
  routers.forEach(r => {
    const menu = { ...r }
    allMenus.push(menu)
    r.children && r.children.length && getAllMenus(r.children)
  })
}

getAllMenus(routers)

routers.forEach(r => {
  if (r.children && r.children.length) {
    rootMenu.push({
      name: r.name,
      title: r.meta.title || r.children[0].meta.title,
      children: [],
      path: '',
      index: 0
    })
  }
})

rootMenu.forEach((r, i) => {
  r.index = i
})

const getMenuList = (routers, parent) => {
  routers.forEach(r => {
    if (r?.meta?.path) {
      menuList.push({
        name: r.name || r.meta.name,
        title: r.meta.title,
        path: r.meta.path,
        pname: parent ? parent.name : null,
        children: [],
        index: 0
      })
    } else if (r.children && r.children.length) {
      getMenuList(r.children, r)
    }
  })
}

getMenuList(routers)

menuList.forEach(m => {
  const root = rootMenu.find(r => r.name === m.pname)
  const parent = menuList.find(r => r.name === m.pname)
  if (root) {
    root.children.push(_.assign(m, { index: root.children.length }))
  } else if (parent) {
    parent.children.push(_.assign(m, { index: parent.children.length }))
  }
})

// console.log('========= menuList:', menuList)
// console.log('========= rootMenu:', rootMenu)
// console.log('========= allMenus:', allMenus)

export default routers
export { menuList, rootMenu, allMenus }
